/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javamail.sdp;

import java.util.List;
import java.util.ListIterator;
import java.util.StringTokenizer;
import java.util.Vector;
import javamail.sdp.util.eSdpMediaType;
import javamail.sdp.util.codecInfo;
import javamail.sdp.util.eRtpStaticPayload;
import javamail.sdp.util.eSdpHeader;
import javamail.sdp.util.mediaLine;

/**
 *
 * @author sriram
 */
public class sdpMessage {
    public String version = null;
    public String originator = null;
    public String session = null;
    public String connect = null;
    public String time = null;
    public List commAttr = null;
    public List mediaList = null;
    
    public sdpMessage() {
        version = new String("0");
        originator = new String("");
        session = new String("");
        connect = new String("");
        time = new String("0 0");
        commAttr = new Vector<String>(1);
        mediaList = new Vector<mediaLine>(1);
    }
    
    public String getSessionId() {
        String result = null;
        if(originator == null)
            return null;
        StringTokenizer tokenizer = new StringTokenizer(originator);
        for(int i=0; tokenizer.hasMoreTokens(); i++) {
            result = tokenizer.nextToken();
            switch(i) {
                case 1:
                    return result;
                default:
                    break;
            }
        }
        return null;
    }

    @Override
    public String toString() {
        String result = new String("");
        if(!version.equals(""))
            result += eSdpHeader.VERSION+"="+version+"\r\n";
        if(!originator.equals(""))
            result += eSdpHeader.ORIGINATOR+"="+originator+"\r\n";
        if(!session.equals(""))
            result += eSdpHeader.NAME+"="+session+"\r\n";
        if(!connect.equals(""))
            result += eSdpHeader.CONNECT+"="+connect+"\r\n";
        if(!time.equals(""))
            result += eSdpHeader.TIME+"="+time+"\r\n";
        ListIterator<String> caList = commAttr.listIterator();
        while(caList.hasNext())
            result += eSdpHeader.ATTRIBUTE+"="+caList.next()+"\r\n";
        ListIterator<mediaLine> list = mediaList.listIterator();
        while(list.hasNext()) {
            mediaLine temp = list.next();
            result += eSdpHeader.MEDIA+"="+
                    temp.media.toString() + " " +
                    temp.port + " " +
                    temp.profile + " ";

            ListIterator<codecInfo> codecList = temp.codecList.listIterator();
            while(codecList.hasNext()) {
                result += codecList.next().payloadNumber + " ";
            }
            result += "\r\n";
            codecList = temp.codecList.listIterator();
            while(codecList.hasNext()) {
                codecInfo tempCodec = codecList.next();
                if(tempCodec.rtpmap!=null && !tempCodec.rtpmap.trim().equals("")) {
                    result += eSdpHeader.ATTRIBUTE+"=rtpmap:"+
                            tempCodec.payloadNumber+" "+tempCodec.rtpmap+"\r\n";
                } 
                if(tempCodec.fmtp!=null && !tempCodec.fmtp.trim().equals("")) {
                    result += eSdpHeader.ATTRIBUTE+"=fmtp:"+
                            tempCodec.payloadNumber+" "+tempCodec.fmtp+"\r\n";
                }
            }
            //result += "\r\n";
        }
        return result;
    }
    
    public List getCodecs(eSdpMediaType medType) {
        List codecs = new Vector(1);
        List temp = null;
        ListIterator<mediaLine> mediaIterator = mediaList.listIterator();
        mediaLine tempMedia = null;
        while(mediaIterator.hasNext()) {
            tempMedia = mediaIterator.next();
            // Add the codecList for this mediaIterator line only if it matches
            if(tempMedia.media == medType) {
                temp = tempMedia.codecList;
            }
        }
        ListIterator<codecInfo> cIterator = temp.listIterator();
        while(cIterator.hasNext()) {
            codecs.add(eRtpStaticPayload.fromPayload(cIterator.next().payloadNumber));
        }
        return codecs;
    }
}
